package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.NoticeAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityNotice;
import com.yuwj.chinaa.entity.EntityPage;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NoticeListActivity extends BaseActivity {


    List<EntityNotice> entityNoticeList;
    NoticeAdapter NoticeAdapter;
    IPresenter presenter;

    EntityPage entityPage;
    boolean isRefresh;
    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.srl_list)
    SwipeRefreshLayout srlList;

    public static Intent createIntent(Context context) {
        return new Intent(context, NoticeListActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_notice_list;
    }

    @Override
    public void initParam() {
        entityPage = new EntityPage();
        presenter = new PresenterImpl(this);
        entityNoticeList = new ArrayList<>();
        NoticeAdapter = new NoticeAdapter(context, entityNoticeList);
    }

    @Override
    public void initView() {
        tvTitle.setText("消息列表");
        rvList.setAdapter(NoticeAdapter);
        srlList.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        NoticeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);
        rvList.setLayoutManager(new LinearLayoutManager(context));
        NoticeAdapter.setEmptyView(R.layout.content_recyclerview_empty, (ViewGroup) rvList.getParent());
        NoticeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(NoticeDetailActivity.createIntent(context, (EntityNotice) adapter.getItem(position)));
            }
        });
        NoticeAdapter.disableLoadMoreIfNotFullPage();
    }

    @Override
    public void initOperator() {
        refresh();
    }

    private void refresh() {
        srlList.setRefreshing(false);
        isRefresh = true;
        entityPage.setPageNo(0);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("pageNo", entityPage.getComputeNextPage() + "");
            jsonObject.put("pageSize", entityPage.getPageSize() + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getNoticeList(jsonObject.toString());

    }

    private void loadMore() {
        isRefresh = false;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("pageNo", entityPage.getComputeNextPage() + "");
            jsonObject.put("pageSize", entityPage.getPageSize() + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getNoticeList(jsonObject.toString());
    }


    @Override
    public void onGetNoticeListSuccess(List<EntityNotice> entityNoticeList) {
        super.onGetNoticeListSuccess(entityNoticeList);
        if (isRefresh) {
            this.entityNoticeList.clear();
        }
        this.entityNoticeList.addAll(entityNoticeList);
        NoticeAdapter.notifyDataSetChanged();

        entityPage.setPageNo(entityPage.getComputeNextPage());
        if (entityNoticeList.size() < entityPage.getPageSize()) {
            NoticeAdapter.loadMoreEnd();
        } else {
            NoticeAdapter.loadMoreComplete();
        }
        NoticeAdapter.loadMoreComplete();
    }

    @Override
    public void onGetNoticeListFail(Exception e) {
        super.onGetNoticeListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage() + "");
        NoticeAdapter.loadMoreFail();
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
